package com.zhuss.product.processor;

import java.util.Date;

import com.zhuss.bean.vo.product.line.DetailLineVo;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.product.db.dao.ext.LineStrategyDao;
import com.zhuss.product.processor.cache.LineCache;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.product.line.DeleteLineBo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.product.db.dao.ext.LineDao;
import com.zhuss.product.db.entity.Line;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.DzcxException;
import com.zhuss.util.DateUtil;
import com.zhuss.util.JsonUtil;

/**
 * @description: 线路删除
 * @author: zhushanshuo
 * @create: Wed Dec 30 13:08:49 CST 2020
 **/
@Component("lineDelete")
public class LineDelete extends AbstractProcessor<Object, DeleteLineBo> implements Processor<Object, DeleteLineBo> {

    @Autowired
    private LineDao lineDao;

    @Autowired
    @Qualifier("lineRead")
    private LineRead lineRead;

    @Autowired
    @Qualifier("lineCache")
    private LineCache lineCache;

    @Autowired
    private LineStrategyDao lineStrategyDao;


    /**
     * 校验
     */
    @Override
    public void check(DeleteLineBo bo) throws DzcxException {

        if (bo.getLineIdList() == null) {
            throw new DzcxException(ExceptionCode.请选择数据);
        }

        //查询线路下是否有运营的策略，有不能暂停或删
        Date now = DateUtil.toDateOnly(new Date());
        if (lineStrategyDao.listByLineId(bo.getLineIdList().get(0), now) > 0) {
            throw new DzcxException(ExceptionCode.该线路下有运营的车次);
        }

    }

    /**
     * 操作方法
     */
    @Override
    public Object process(DeleteLineBo param) throws DzcxException {
        for (Long lineId : param.getLineIdList()) {
            String name = ((DetailLineVo) lineRead.detail(lineId)).getName();
            Line record = new Line();
            record.setLineId(lineId);
            record.setDeleted(param.getDeleted());
            record.setModifierId(param.getConcurrentUserId());
            record.setModifier(param.getConcurrentUserName());
            record.setModifyTime(new Date());
            lineDao.delete(record);
            DeleteLineBo bo = new DeleteLineBo();
            bo.setLineId(lineId);
            bo.setName(name);

            lineCache.clear(lineId);
            optLog(bo);
        }
        return null;
    }

    /**
     * 操作日志
     */
    public Object optLog(DeleteLineBo param) throws DzcxException {
        OpLog record = new OpLog();
        record.setMoudle(MoudleEnum.商品.key());
        record.setAction(LogActionEnum.线路删除.key());
        record.setTargetId(param.getLineId());
        record.setTargetName(param.getName());
        record.setContent("参数: " + JsonUtil.toJson(param));
        record.setReason(param.getExceptionReason());

        record.setCreatorId(param.getConcurrentUserId());
        record.setCreator(param.getConcurrentUserName());
        record.setCreateTime(new Date());
        log(record);
        return null;
    }

    @Override
    public Object log(DeleteLineBo deleteLineBo) throws DzcxException {
        return null;
    }
}
